Skip to content

Add use_causal_mask to MultiHeadAttention and approximate GELU - #466

Closed
Dobiasd wants to merge 1 commit into
masterfrom
add-mha-causal-mask-and-gelu-approximate
Closed

Add use_causal_mask to MultiHeadAttention and approximate GELU#466
Dobiasd wants to merge 1 commit into
masterfrom
add-mha-causal-mask-and-gelu-approximate

Conversation

@Dobiasd

@Dobiasd Dobiasd commented May 3, 2026

Copy link
Copy Markdown
Owner

Summary

  • MultiHeadAttention now respects use_causal_mask=True. The converter extracts the flag from the layer's first inbound node (it's a per-call kwarg, not stored in get_config()), bakes it into the JSON, and the C++ runtime applies a triangular mask on the score tensor before softmax.
  • gelu gains an approximate=True path (tanh form). A serializable gelu_approximate helper in convert_model lets Keras models reference it via activation=gelu_approximate; the converter rewrites the activation to plain "gelu" with approximate=True in the layer config, and the runtime picks the right formula. Existing exact-GELU users are unaffected (default approximate=False).
  • Both features are now exercised by test_model_exhaustive (three new MHA cases with use_causal_mask=True, two new Dense cases for exact / approximate GELU).

Together these are the runtime building blocks needed to load decoder-only LLMs (e.g. GPT-2) into frugally-deep. The actual GPT-2 example tooling is split out into a follow-up PR to keep this one focused.

Test plan

  • test_model_exhaustive passes (with new MHA causal-mask + GELU approximate cases)
  • test_model_embedding, test_model_recurrent, test_model_sequential, test_model_variable, test_model_autoencoder all still pass after the converter changes (they invoke rewrite_custom_activations and inject_mha_call_kwargs even when no rewrites apply).

🤖 Generated with Claude Code

The MultiHeadAttention layer now respects the per-call use_causal_mask=True
kwarg: scores at (t, k) for k > t are set to -inf before softmax. The
converter extracts the flag from the layer's first inbound node and bakes it
into the JSON config so the runtime can pick it up.

GELU gains the tanh-approximation form. A serializable gelu_approximate
helper in convert_model lets Keras models reference it via
activation=gelu_approximate; the converter rewrites that to a plain "gelu"
activation with approximate=True in the config, which the C++ gelu_layer
applies via the appropriate formula.

Both features are exercised by new cases in the exhaustive test model.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Dobiasd Dobiasd mentioned this pull request May 3, 2026
5 tasks
@Dobiasd

Dobiasd commented May 3, 2026

Copy link
Copy Markdown
Owner Author

Closing — landed as an experiment, not pursuing for merge.

@Dobiasd Dobiasd closed this May 3, 2026
@Dobiasd
Dobiasd deleted the add-mha-causal-mask-and-gelu-approximate branch May 3, 2026 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant